const { ccclass, property } = cc._decorator;

import MatchGameConfig from "./MatchGameConfig";
import BottomPrefab from "./bottomPrefab";

@ccclass
export default class BottomPrefabCtrl extends cc.Component {
    ui = null;

    onLoad() {
        this.onUILoad();
        this.addButtonListen();
    }

    start() {}

    onUILoad() {
        this.ui = this.node.addComponent(BottomPrefab);
    }

    addButtonListen() {}

    initData() {}

    init(e, t, a) {
        let o = this;
        let i = this.checKIsSide(e, t, a);

        this.ui.BottomBgItem.children.forEach( (e) => {
            e.active = true;
        });

        this.ui.mapBg1.active = true;

        if (i && i.length != 0) {
            i.forEach( (e) => {
                o.ui.BottomBgItem.getChildByName(`line_${e}`).active = false;
            });

            // this.ui.point_4.active = this.ui.line_1.active && this.ui.line_4.active;
            // this.ui.point_3.active = this.ui.line_1.active && this.ui.line_2.active;
            // this.ui.point_2.active = this.ui.line_2.active && this.ui.line_3.active;
            // this.ui.point_1.active = this.ui.line_3.active && this.ui.line_4.active;
        }
    }

    checKIsSide(e, t, a) {
        let o = [];

        if (e != 0 && a[e - 1][t] === MatchGameConfig.DisplayMapType.Normal) {
            o.push(1);
        }

        if (t != 0 && a[e][t - 1] === MatchGameConfig.DisplayMapType.Normal) {
            o.push(2);
        }

        if (e != a.length - 1 && a[e + 1][t] === MatchGameConfig.DisplayMapType.Normal) {
            o.push(3);
        }

        if (t != a[e].length - 1 && a[e][t + 1] === MatchGameConfig.DisplayMapType.Normal) {
            o.push(4);
        }

        return o;
    }

    getMaskArr(e, t, a) {
        let o = [];
        let i = [];
        let r = [];
        let l = [];
        let s = [];
        let c = [];

        if (e == 0 || a[e - 1][t] == MatchGameConfig.DisplayMapType.EmptyDrop) {
            r.push(cc.v2(this.ui.corner1.x + this.node.x, this.ui.corner1.y + this.node.y));
            r.push(cc.v2(this.ui.corner2.x + this.node.x, this.ui.corner2.y + this.node.y));
        }

        if (t == 0 || a[e][t - 1] == MatchGameConfig.DisplayMapType.EmptyDrop) {
            l.push(cc.v2(this.ui.corner2.x + this.node.x, this.ui.corner2.y + this.node.y));
            l.push(cc.v2(this.ui.corner3.x + this.node.x, this.ui.corner3.y + this.node.y));
        }

        if (e == a.length - 1 || a[e + 1][t] == MatchGameConfig.DisplayMapType.EmptyDrop) {
            s.push(cc.v2(this.ui.corner3.x + this.node.x, this.ui.corner3.y + this.node.y));
            s.push(cc.v2(this.ui.corner4.x + this.node.x, this.ui.corner4.y + this.node.y));
        }

        if (t == a[e].length - 1 || a[e][t + 1] == MatchGameConfig.DisplayMapType.EmptyDrop) {
            c.push(cc.v2(this.ui.corner4.x + this.node.x, this.ui.corner4.y + this.node.y));
            c.push(cc.v2(this.ui.corner1.x + this.node.x, this.ui.corner1.y + this.node.y));
        }

        if (r.length || s.length) {
            if (l.length || c.length) {
                if (l.length) {
                    if (s.length) {
                        r.length && (o = o.concat(r));
                        l.length && (o = o.concat(l));
                        s.length && (o = o.concat(s));
                        c.length && (o = o.concat(c));
                    } else {
                        c.length && (o = o.concat(c));
                        r.length && (o = o.concat(r));
                        l.length && (o = o.concat(l));
                    }
                } else {
                    s.length && (o = o.concat(s));
                    c.length && (o = o.concat(c));
                    r.length && (o = o.concat(r));
                }

                if (o.length == 0) return null;

                for (let u = 0; u < o.length; u++) {
                    for (let p = u + 1; p < o.length; p++) {
                        if (o[u].x == o[p].x && o[u].y == o[p].y) {
                            o.splice(p, 1);
                            p--;
                        }
                    }
                }
            } else {
                o = o.concat(r);
                i = i.concat(s);
            }
        } else {
            o = o.concat(l);
            i = i.concat(c);
        }

        return {
            index1: o,
            index2: i
        };
    }

    unique(e) {
        return Array.from(new Set(e));
    }
}
